
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class ChatFilter {

    private ArrayList<String> targets = new ArrayList();
    private ArrayList<String> replacements = new ArrayList();
    private String name;

    public ChatFilter(String name) {
        this.name = name;
    }

    // Filter the text
    public String filter(String text) {
        StringBuilder builder = new StringBuilder(text.length() * 2);
        // Split at word boundaries
        String[] words = text.split("\\b");

        for (String word : words) {
            boolean replaced = false;
            for (int i = 0; i < targets.size(); i++) {
                String target = targets.get(i);
                // Replace the word if it is a target
                if (word.equalsIgnoreCase(target)) {
                    String replacement = replacements.get(i);
                    if (Character.isUpperCase(word.charAt(0)) == false) {
                        // Word isn't capitalized
                        builder.append(replacement);
                    } else {
                        if (word.matches("[A-Z]{" + word.length() + "}")) {
                            // Word is all caps
                            builder.append(replacement.toUpperCase());
                        } else {
                            // Only first char capitalized
                            builder.append(Character.toUpperCase(replacement.charAt(0)));
                            builder.append(replacement.substring(1));
                        }
                    }
                    replaced = true;
                    break;
                }
            }
            if (replaced == false) {
                // None of the targets were matched
                builder.append(word);
            }

        }
        return builder.toString();
    }

    // Add a replacement filter
    public void addFilter(String target, String replacement) {
        targets.add(target);
        replacements.add(replacement);
    }

    public void getFiltersFromFile(File f) throws IOException {
        BufferedReader in = new BufferedReader(new FileReader(f));
        String line;
        while ((line = in.readLine()) != null) {
            line = line.trim();
            String[] splitLine = line.split(": ");
            String target = splitLine[0];
            String replacement = splitLine[1].replace("\"", ""); // remove quotes
            addFilter(target, replacement);
        }

    }

    public static ChatFilter[] getDefaultFilters() {
        return new ChatFilter[]{
                    DEFAULT,
                    LOLSPEAKZ,
                    SOPHISTICATION,
                    ALL_CAPS,
                    SDRAWKCAB
                };
    }

    @Override
    public String toString() {
        return name;
    }
    /*
     * The following initializes four deafult chat filters. Really, these should
     * be made into separate classes, but I hate to clutter up the source code
     * with additional classes just for chat filtering, which are not relavant
     * to the functionality of this app.
     */
    public static final ChatFilter DEFAULT;
    public static final ChatFilter SOPHISTICATION;
    public static final ChatFilter LOLSPEAKZ;
    public static final ChatFilter ALL_CAPS;
    public static final ChatFilter SDRAWKCAB;

    static {
        // Create default filter
        DEFAULT = new ChatFilter("None") {

            @Override
            public String filter(String text) {
                // The default filter does nothing to change the text
                return text;
            }
        };

        // create sophistication filter
        SOPHISTICATION = new ChatFilter("Sophistication");
        try {
            File filterFile = new File(ChatFilter.class.getResource("sophistication.yml").toURI());
            SOPHISTICATION.getFiltersFromFile(filterFile);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Create lolspeak filter
        LOLSPEAKZ = new ChatFilter("Lolspeakz") {

            @Override
            public String filter(String text) {
                text = super.filter(text);
                text = text.replace("!", "!" + randomAnnoyance());
                return text;
            }

            // Herp derp!!1one
            private String randomAnnoyance() {
                String orly = ""; // ya rly
                double annoyNum = Math.random(); // no wai
                double annoyType; // yaweah
                for (int i = 0; i < annoyNum * 4; i++) {
                    annoyType = Math.random();
                    if (annoyType < .35) {
                        orly += "!";
                    } else if (annoyType < .7) {
                        orly += "1";
                    } else {
                        orly += "one";
                    }
                }
                return orly;
            }
        };

        try {
            // try to get the location of this class's directory
            File filterFile = new File(ChatFilter.class.getResource("lolspeakz.yml").toURI());
            LOLSPEAKZ.getFiltersFromFile(filterFile);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // create all caps filter
        ALL_CAPS = new ChatFilter("All caps") {

            @Override
            public String filter(String s) {
                return s.toUpperCase();
            }
        };

        // create backwards filter
        SDRAWKCAB = new ChatFilter("sdrawkcaB") {

            @Override
            public String filter(String text) {
                // This is so stupid I used a recursive function
                if (text.isEmpty()) {
                    return "";
                }
                return filter(text.substring(1)) + text.substring(0, 1);
            }
        };

    }
}
